home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Frameworks / Hsoi's App Shell 1.0a4 / Hsoi's App Shell Source / HASUtilTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-28  |  6.8 KB  |  345 lines  |  [TEXT/CWIE]

  1. /*
  2.     HASUtilTest.c from Hsoi's App Shell ©1995-1997 John C. Daub. All rights reserved.
  3.     
  4.     This file contains various utility functions for use with the HAS Test code to
  5.     help you display various things in the message window.
  6. */
  7.  
  8. #pragma mark ••• #includes •••
  9.  
  10. #ifndef __HSOIS_APP_SHELL__
  11. #include "HASMain.h"
  12. #endif
  13. #include "HASGlobals.h"
  14. #include "HASMenus.h"
  15. #include "HASMenuWindows.h"
  16. #include "HASSoundSpeech.h"
  17. #include "HASTest.h"
  18. #include "HASUtilTest.h"
  19.  
  20. #pragma mark -
  21. #pragma mark ••• Test Window Creation/Destruction •••
  22.  
  23. // this loads up and prepares our test window
  24.  
  25. WindowRef    HsoiGetTestWindow( void )
  26. {
  27.  
  28.     // get our test window if we don't already have it
  29.     
  30.     if ( gTestWindow == nil )
  31.         gTestWindow = GetNewWindow( rTestWIND, nil, MOVE_TO_FRONT );
  32.     
  33.     // some crude error handling
  34.     
  35.     if ( gTestWindow == nil )
  36.     {
  37.         SysBeep( 5 );
  38.         return nil;
  39.     }
  40.     
  41.     // set the port
  42.     
  43.     SetPortWindowPort( gTestWindow );
  44.     
  45.     // set the windowKind for identification
  46.     
  47.     SetWindowKind( gTestWindow, kTestWindowKind );
  48.     
  49.     // make the window visible
  50.     
  51.     ShowWindow( gTestWindow );
  52.     
  53.     // bring it to the front (if it's in the background)
  54.     
  55.     SelectWindow( gTestWindow );
  56.     
  57.     // adjust the menus to suit
  58.     
  59.     HsoiAdjustMenus();
  60.     
  61.     // and return the window
  62.     
  63.     return gTestWindow;
  64. }
  65.  
  66. // this will remove the test window from existance on this earth
  67.  
  68. void    HsoiDestroyTestWindow( void )
  69. {
  70.     // simple enough...just dispose it (nice there's nothing attached to the window )
  71.     
  72.     DisposeWindow( gTestWindow );
  73.     
  74.     // set the WindowRef to nil for better error checking (using nil variables
  75.     // usually turns up bad consequences)
  76.     
  77.     gTestWindow = nil;
  78.     
  79.     // and adjust the menus to fit
  80.     
  81.     HsoiAdjustMenus();
  82.     
  83.     return;
  84. }
  85.  
  86. #pragma mark -
  87. #pragma mark ••• Test Window Utils •••
  88.  
  89. // checks the window passed to it to see if it's the test window
  90.  
  91. Boolean    HsoiIsTestWindow( WindowRef window )
  92. {
  93.     if ( window == nil )
  94.         return false;
  95.     
  96.     else
  97.         return ( GetWindowKind(window) == kTestWindowKind );
  98. }
  99.  
  100.  
  101. // our routine to adjust the menus when the test window is frontmost
  102.  
  103. void    HsoiAdjustMenusTestWindow( WindowRef window )
  104. {
  105.     MenuRef        menu;
  106.     short        i;
  107.     
  108.     // Apple Menu -- enable it all
  109.     
  110.     menu = GetMenuHandle( mApple );
  111.     
  112.     for ( i = 0; i <= CountMenuItems( menu ); i++ )
  113.         EnableItem( menu, i );
  114.     
  115.     // File Menu
  116.     
  117.     menu = GetMenuHandle( mFile );
  118.     for ( i = 0; i <= CountMenuItems( menu ); i++ )
  119.         EnableItem( menu, i );
  120.  
  121.     // if we have hit the max number of open document windows, we must disable New
  122.     // and Open
  123.     
  124.     if ( gNumWindows >= kMaxNumberOfOpenWindows )
  125.     {
  126.         DisableItem( menu, iNew );
  127.         DisableItem( menu, iOpen );
  128.     }
  129.     
  130.     DisableItem( menu, iSave );
  131.     DisableItem( menu, iSaveAs );
  132.     DisableItem( menu, iRevert );
  133.     DisableItem( menu, iPrint );
  134.     
  135.     // Edit Menu -- not much to do here either...
  136.     
  137.     menu = GetMenuHandle( mEdit );
  138.     for ( i = 0; i <= CountMenuItems( menu ); i++ )
  139.         EnableItem( menu, i );
  140.     
  141.     DisableItem( menu, iUndo );
  142.     DisableItem( menu, iCut );
  143.     DisableItem( menu, iCopy );
  144.     DisableItem( menu, iPaste );
  145.     DisableItem( menu, iClear );
  146.     DisableItem( menu, iSelectAll );
  147.     
  148.     // Text Menu (and it's submenus...cause this is pretty straight forward)
  149.     
  150.     DisableItem( GetMenuHandle( mText ), 0 );
  151.     DisableItem( GetMenuHandle( mFont ), 0 );
  152.     DisableItem( GetMenuHandle( mSize ), 0 );
  153.     DisableItem( GetMenuHandle( mStyle ), 0 );
  154.     DisableItem( GetMenuHandle( mJustification ), 0 );
  155.     DisableItem( GetMenuHandle( mColor ), 0 );
  156.     DisableItem( GetMenuHandle( mFeatures ), 0 );
  157.     
  158.     // Dialogs Menu
  159.     
  160.     menu = GetMenuHandle( mDialogs );
  161.     for ( i = 0; i <= CountMenuItems( menu ); i++ )
  162.         EnableItem( menu, i );
  163.     
  164.     // Windows menu
  165.     
  166.     menu = GetMenuHandle( mWindows );
  167.     for ( i = 0; i <= CountMenuItems( menu ); i++ )
  168.         EnableItem( menu, i );
  169.     HsoiAdjustWindowsMenu( window );
  170.     
  171.     // Sound menu (and Voices submenu)
  172.     
  173.     HsoiAdjustSoundMenu( window );
  174.     
  175.     // Help menu
  176.     
  177.     HMGetHelpMenuHandle( &menu );
  178.     if ( gHiliting )
  179.         DisableItem( menu, 0 );
  180.     else
  181.     {
  182.         for ( i = 0; i <= CountMenuItems( menu ); i++ )
  183.             EnableItem( menu, i );
  184.     }
  185.  
  186.     // Test menu
  187.     
  188.     menu = GetMenuHandle( mTestMENU );
  189.     if ( gHiliting )
  190.         DisableItem( menu, 0 );
  191.     else
  192.     {
  193.         for ( i = 0; i <= CountMenuItems( menu ); i++ )
  194.             EnableItem( menu, i );
  195.     }
  196.         
  197.  
  198.  
  199.     return;
  200. }
  201.  
  202. #pragma mark -
  203. #pragma mark ••• Misc Testing Utils •••
  204.  
  205. // what follows are various utility routines nice for helping you do things
  206. // with the test code and window.  note how many are hard coded to use the test window.
  207. // if you're going to use any of these functions, you need to make sure that you've
  208. // already gotten the gTestWindow, possibly stopped any currently playing sounds, etc.
  209. // see HsoiDoTest() for a sample.
  210.  
  211. void    HsoiShowError( Str255 errorMessage, long errorNumber )
  212. {
  213.     WindowRef        window;
  214.     Str255            numberString;
  215.     
  216.     NumToString( errorNumber, numberString );
  217.     
  218.     GetWindowPort( &window );
  219.     
  220.     SetPortWindowPort( gTestWindow );
  221.     EraseRect( &GetWindowPort(gTestWindow)->portRect );
  222.     MoveTo( 10, 20 );
  223.     
  224.     DrawString( errorMessage );
  225.     DrawString( numberString );
  226.     
  227.     SetPortWindowPort( window );
  228.     
  229.     return;
  230. }
  231.  
  232.  
  233. //  PrintOSType: display an OSType string in the test window
  234. void    HsoiPrintOSType( OSType theType )
  235. {
  236.     Str255        typeString;
  237.     WindowRef    window;
  238.     short        i;
  239.     
  240.     typeString[0] = 4;
  241.     
  242.     for ( i = 0; i <= 3; i++ )
  243.     {
  244.         typeString[ 4 - i ] = (char)(theType & 0x000000FF );
  245.         theType = theType >> 8;
  246.     }
  247.     
  248.     GetWindowPort( &window );
  249.     
  250.     SetPortWindowPort( gTestWindow );
  251.     EraseRect( &GetWindowPort(gTestWindow)->portRect );
  252.     MoveTo( 10, 20 );
  253.     DrawString( typeString );
  254.     
  255.     SetPortWindowPort( window );
  256.     
  257.     return;
  258. }
  259.  
  260.  
  261. //  PrintString: display a string in the test Window
  262.  
  263. void    HsoiPrintString( Str255 s )
  264. {
  265.     WindowRef        window;
  266.     
  267.     if ( !s )
  268.         return;
  269.         
  270.     GetWindowPort( &window );
  271.     
  272.     SetPortWindowPort( gTestWindow );
  273.     EraseRect( &GetWindowPort(gTestWindow)->portRect );
  274.     MoveTo( 10, 20 );
  275.     
  276.     DrawString( s );
  277.     
  278.     SetPortWindowPort( window );
  279.     
  280.     return;
  281. }
  282.  
  283.  
  284. // PrintHex:  convert number to a hex string and display in test Window
  285.  
  286. void    HsoiPrintHex( long theNumber )
  287. {
  288.     char        theString[10];
  289.     long        digit;
  290.     short        i;
  291.     WindowRef    window;
  292.     
  293.     GetWindowPort( &window );
  294.     
  295.     SetPortWindowPort( gTestWindow );
  296.     EraseRect( &GetWindowPort(gTestWindow)->portRect );
  297.     MoveTo( 10, 20 );
  298.     
  299.     theString[ 0 ] = 9;
  300.     theString[ 1 ] = '$';
  301.     
  302.     for ( i = 0; i <= 7; i++ )
  303.     {
  304.         digit = theNumber & 0x0000000F;
  305.  
  306.         if ( digit < 10 )
  307.         {
  308.             digit += (long)('0');
  309.         }
  310.         else
  311.         {
  312.             digit += (long)( 'A' - 10 );
  313.         }
  314.         
  315.         theString[ 9 - i ] = (char)digit;
  316.         theNumber = theNumber >> 4;
  317.     }
  318.     
  319.     DrawString( (StringPtr)theString );
  320.  
  321.     SetPortWindowPort( window );
  322.     
  323.     return;
  324. }
  325.  
  326.  
  327. //    CRLF: advance drawing position in front window to left side of next line
  328.  
  329. void    HsoiCRLF( void )
  330. {
  331.  
  332.     Point            currentPosition;
  333.     FontInfo        theFontInfo;
  334.     register short    lineHeight;
  335.     
  336.     GetPen( ¤tPosition );
  337.     
  338.     GetFontInfo( &theFontInfo );
  339.     lineHeight = theFontInfo.ascent + theFontInfo.descent + theFontInfo.leading;
  340.     
  341.     MoveTo( kLeftMargin, currentPosition.v + lineHeight );
  342.     
  343.     return;
  344. }
  345.